﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMD_DKT.DAL;
using System.Data;
using System.Data.SqlClient;

namespace CMD_DKT.BAL
{
    public class Bitacora
    {
        DataTable dtEvents;

        public DataTable DtEvents
        {
            get { return dtEvents; }
            set { dtEvents = value; }
        }

        public Bitacora()
        {
            this.dtEvents = new DataTable();

            DataColumn dc = new DataColumn("MSG", typeof(string));
            dtEvents.Columns.Add(dc);
            dc = new DataColumn("DATELOG", typeof(DateTime));
            dtEvents.Columns.Add(dc);
            dc = new DataColumn("KEYWORD", typeof(string));
            dtEvents.Columns.Add(dc);

        }

        public  void GuardaMensaje(string mensaje, string keyword)
        {
            this.dtEvents.Rows.Add(mensaje, DateTime.Now, keyword);
        }

        public  void InserStackMessage()
        {
            string msg, key;
            DateTime date;
            DataView dv = this.DtEvents.DefaultView;
            dv.Sort = "DATELOG";
            this.DtEvents = dv.ToTable();

            foreach (DataRow item in DtEvents.Rows)
            {
                msg = item["MSG"].ToString();
                key = item["KEYWORD"].ToString();
                date = DateTime.Parse(item["DATELOG"].ToString());
                string format = "yyyyMMdd  hh:mm:ss.ff";
                string fecha = date.ToString(format);
                
                string query = "SP_CMD_SOL_INSERT_LOG '" + msg +"','"+ fecha +"','"+ key+"'";

                try
                {
                    SrvDB.ExecuteNonQuery(query);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }//foreach
        }

    }//end class
}
